using System;
using System.Collections.Generic;
using System.Linq;
using Grundy.Common;
using Grundy.CryptographicServices;
using Grundy.Entity;
using Grundy.Message;

namespace Grundy.TestHelpers
{
    public class ObjectMother
    {
        public CustomerEntitlement GetCustomerEntitlement(List<LicenseEntitlement> licenseEntitlements, Customer customer)
        {
            return new CustomerEntitlement
            {
                Customer = customer,
                LicenseEntitlements = new LicenseEntitlementCollection(licenseEntitlements)
            };
        }

        public License GetCountedFloatingLicense(long limit)
        {
            return new CountedFloatingLicense
            {
                Id = Guid.NewGuid(),
                Client = LicenseClientType.Checkout,
                Limit = limit,
                Name = "Floating License"
            };
        }

        public License GetNodeLockedLicense()
        {
            return new NodeLockedLicense
            {
                Id = Guid.NewGuid(),
                Name = "Node Locked License",
            };
        }

        private IdentifierCollection GetIdentifierCollection(IEnumerable<Tuple<string, string>> identifiers)
        {
            return new IdentifierCollection(identifiers.Select(i => new ProcessIdentifier { Value = i.Item2 }));
        }

        public LicenseEntitlement GetLicenseEntitlement(Product product, License license)
        {
            return new LicenseEntitlement { Id = Guid.NewGuid(), Product = product, License = license, Node = GetNode(), CreatedOn = Clock.Instance.Utc};
        }

        private Node GetNode()
        {
            var name = "New Node " + new Random().Next();
            return new Node
            {
                Id = Guid.NewGuid(),
                Name = name,
                Identifiers = new IdentifierCollection(new[] { new HostnameIdentifier { Value = name } })
            };
        }

        public Product GetProduct(string productName, string[] featureNames)
        {
            var features = featureNames.Select(i => new Feature { Id = Guid.NewGuid(), Name = i });
            return new Product { Id = Guid.NewGuid(), Name = productName, Features = new FeatureCollection(features) };
        }

        public Customer GetCustomer(string name)
        {
            return new Customer { Id = Guid.NewGuid(), Name = name };
        }
    }
}